package LeetCode_160;

/**
 * @author : niu niu
 * @version : 1.0.0
 * @date : 2022/10/30 0:52
 */

class ListNode {
      int val;
      ListNode next;
      ListNode(int x) {
         val = x;
          next = null;
     }
  }
public class Solution {
    private static int sizeOf(ListNode head){
        int size = 0;
        for (ListNode cur = head; cur !=null ; cur=cur.next) {
            size++;
        }
        return size;
    }
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode l1 = headA;
        ListNode l2 =headB;
        int c1 = sizeOf(headA);
        int c2 = sizeOf(headB);

        if (c1>c2){
            for (int i = 0; i < c1-c2; i++) {
                l1 =l1.next;
            }
        }else {
            for (int i = 0; i < c2 - c1; i++) {
                l2= l2.next;
            }
        }

        while (l1 != l2){
            l1=l1.next;
            l2=l2.next;
        }
        return l1;
    }
}